<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>-->
    <!--<script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>-->
    <!--<script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>-->
    <!--[endif]-->
    <script type="text/javascript" src="../../common/js/public.js"></script>
    <script type="text/javascript" src="../../common/js/publicUtils.js"></script>

</head>
<body>
<div class="layui-fluid" id="flowDiv">
    <div class="zTreeBackground right" style="padding-left:1%;width:98%">
        <form class="layui-form" id="datasource" lay-filter="datasource">
            <div class="layui-form-item">
                <label class="layui-form-label">数据库类型</label>
                <div class="layui-input-block">
                    <input type="radio" name="datasourceType" value="1" title="mysql" checked="checked">
                    <input type="radio" name="datasourceType" value="2" title="oracle">
                    <input type="radio" name="datasourceType" value="3" title="sqlserver">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="url"><span style="color: red;">*</span>数据库地址</label>
                <div class="layui-input-block">
                    <input type="text" name="url" id="url" lay-verify="title" autocomplete="off" placeholder="请输入数据库地址或IP" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="port"><span style="color: red;">*</span>端&emsp;口&emsp;号</label>
                <div class="layui-input-block">
                    <input type="text" name="port" id="port" lay-verify="title" autocomplete="off" placeholder="请输入端口号,默认3306/1521/1433" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="database"><span style="color: red;">*</span>数据库/实例</label>
                <div class="layui-input-block">
                    <input type="text" name="database" id="database" lay-verify="title" autocomplete="off" placeholder="请输入数据库名称或数据库实例名" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="loginname"><span style="color: red;">*</span>用&emsp;户&emsp;名</label>
                <div class="layui-input-block">
                    <input type="text" name="loginname" id="loginname" lay-verify="title" autocomplete="off" placeholder="请输入数据库用户名" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="password"><span style="color: red;">*</span>密&emsp;&emsp;&emsp;码</label>
                <div class="layui-input-block">
                    <input type="password" name="password" id="password" lay-verify="title" autocomplete="off" placeholder="请输入数据库登录密码" class="layui-input">
                </div>
            </div>
        </form>
    </div>
</div>
</body>
<SCRIPT type="text/javascript">
    var zTreeObj,jobid,form,tableObj,table,level=0,treeNodeId=-1,selecNode,count=1,hideflag=true;
    $(function(){
        //loadbutton("toolbar");//加载按钮loadbutton(id)，id为button容器id，不传默认为buttongroup
        layui.use(['form','element'],function() {
            form = layui.form;
            var element = layui.element;
            var detaildata = store.get("detaildata");
            form.val("datasource",{
                "url":detaildata.jdbcurl,
                "port":detaildata.port,
                "datasourceType":detaildata.datasourceType,
                "database":detaildata.database,
                "loginname":detaildata.username,
                "password":detaildata.password
            })
            form.render();
        })
    })

    function testconnect(){
        var obj = $(validform().form());
        if(!obj[0]){
            return false;
        }
        var datasourceType = $("input[type=radio][name=datasourceType]:checked").val();
        var jdbcurl = "";
        var driver = "";
        var username = $.trim($("input[name=loginname]").val());
        var password = $.trim($("input[name=password]").val());
        if(datasourceType=='1'){
            driver = "com.mysql.cj.jdbc.Driver";
            jdbcurl = "jdbc:mysql://"+$.trim($("input[name=url]").val())+":"+$.trim($("input[name=port]").val())+"/"+$.trim($("input[name=database]").val())+"?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useOldAliasMetadataBehavior=true";
        }else if(datasourceType=='2'){
            driver = "oracle.jdbc.driver.OracleDriver";
            jdbcurl = "jdbc:oracle:thin:@"+$.trim($("input[name=url]").val())+":"+$.trim($("input[name=port]").val())+":"+$.trim($("input[name=database]").val());
        }else{
            driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
            jdbcurl = "jdbc:sqlserver://"+$.trim($("input[name=url]").val())+":"+$.trim($("input[name=port]").val())+";DatabaseName="+$.trim($("input[name=database]").val());
        }
        var data = {};

        data["datasourceType"]=datasourceType;
        data["jdbcurl"]=jdbcurl;
        data["username"]=username;
        data["password"]=password;
        data["driver"]=driver;
        publicAjax(null,"tool/gen/testConnect",data,null,null,function (r) {
            if(r.code==2000){
                layer.alert("连接成功！");
            }else{
                layer.alert("连接失败，请输入正确账号密码！")
            }
        })
    }

    function datasourceset(){
        var obj = $(validform().form());
        if(!obj[0]){
            return false;
        }
        var datasourceType = $("input[type=radio][name=datasourceType]:checked").val();
        var jdbcurl = "";
        var username = $.trim($("input[name=loginname]").val());
        var password = $.trim($("input[name=password]").val());
        if(datasourceType=='1'){
            jdbcurl = "jdbc:mysql://"+$.trim($("input[name=url]").val())+":"+$.trim($("input[name=port]").val())+"/"+$.trim($("input[name=database]").val())+"?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useOldAliasMetadataBehavior=true";
        }else if(datasourceType=='2'){
            jdbcurl = "jdbc:oracle:thin:@"+$.trim($("input[name=url]").val())+":"+$.trim($("input[name=port]").val())+":"+$.trim($("input[name=database]").val());
        }else{
            jdbcurl = "jdbc:sqlserver://"+$.trim($("input[name=url]").val())+":"+$.trim($("input[name=port]").val())+";DatabaseName="+$.trim($("input[name=database]").val());
        }
        var data = {};
        data["datasourceType"]=datasourceType;
        data["jdbcurl"]=jdbcurl;
        data["username"]=username;
        data["password"]=password;

        var detaildata = {};
        detaildata["datasourceType"]=datasourceType;
        detaildata["jdbcurl"]=$.trim($("input[name=url]").val());
        detaildata["port"]=$.trim($("input[name=port]").val());
        detaildata["database"]=$.trim($("input[name=database]").val());
        detaildata["username"]=username;
        detaildata["password"]=password;

        store.set("datasource",data);
        store.set("detaildata",detaildata);
        return {"code":2000};
    }


    //编写表单验证函数validform，在验证按钮注册按钮点击事件内调用验证函数对象
    function validform(){
        return $("#datasource").validate({
            rules : {
                url:{
                    required:true,
                    normalizer: function( value ) {
                        return $.trim( value );
                    }
                },
                port : {
                    required:true,
                    normalizer: function( value ) {
                        return $.trim( value );
                    }
                },
                database: {
                    required:true,
                    normalizer: function( value ) {
                        return $.trim( value );
                    }
                },
                loginname: {
                    required:true,
                    normalizer: function( value ) {
                        return $.trim( value );
                    }

                },
                password: {
                    required:true,
                    normalizer: function( value ) {
                        return $.trim( value );
                    }
                },
            },
            messages : {
                url : {
                    required: "请输入地址"
                },
                port : {
                    required: "请输入端口"
                },
                database : {
                    required: "请输入数据库或实例"
                },
                loginname : {
                    required:"请输入账号",
                },
                password : {
                    required:"请输入密码",
                }
            }
        });
    }


</script>
</html>